package com.wolfsea.scrolledfloatactionbutton.view
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ViewConfiguration
import android.view.ViewGroup
import com.google.android.material.floatingactionbutton.FloatingActionButton
import kotlin.math.abs
import kotlin.properties.Delegates

/**
 *@desc  可滑动的FloatingActionButton
 *@author liuliheng
 *@time 2021/11/7  23:32
 **/
class ScrolledFloatActionButton(context: Context, attributeSet: AttributeSet) : FloatingActionButton(context, attributeSet) {

    private var mX by Delegates.notNull<Float>()
    private var mY by Delegates.notNull<Float>()

    private var mParentWidth by Delegates.notNull<Int>()
    private var mParentHeight by Delegates.notNull<Int>()

    private var mScrollLeft by Delegates.notNull<Int>()
    private var mScrollTop by Delegates.notNull<Int>()
    private var mScrollRight by Delegates.notNull<Int>()
    private var mScrollBottom by Delegates.notNull<Int>()

    private var hasScroll = false
    private var isScroll = false

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        //防止布局重置时,重置ScrolledFloatActionButton的位置.
        if (parent is ViewGroup) {
           (parent as ViewGroup).addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
               if (hasScroll && mScrollRight != 0 && mScrollBottom != 0) {
                   layout(mScrollLeft, mScrollTop, mScrollRight, mScrollBottom)
               }
           }
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        if (parent is ViewGroup) {
            mParentWidth = (parent as ViewGroup).width
            mParentHeight = (parent as ViewGroup).height
        }
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                mX = ev.x
                mY = ev.y
                super.onTouchEvent(ev)
                return true
            }

            MotionEvent.ACTION_MOVE -> {
                val scaledTouchSlop = ViewConfiguration.get(context).scaledTouchSlop
                var x = ev.x
                var y = ev.y
                x -= mX
                y -= mY
                if (abs(scaledTouchSlop) < abs(x) || abs(scaledTouchSlop) < abs(y)) {

                    isScroll = true
                }
                if (isScroll) {

                    mScrollLeft = (getX() + x).toInt()
                    mScrollTop = (getY() + y).toInt()
                    mScrollRight = (getX() + width + x).toInt()
                    mScrollBottom = (getY() + height + y).toInt()

                    //防止滑出界面
                    if (mScrollLeft < 0 || mScrollRight > mParentWidth) {
                        mScrollLeft = getX().toInt()
                        mScrollRight = getX().toInt() + width
                    }

                    if (mScrollTop < 0 || mScrollBottom > mParentHeight) {
                        mScrollTop = getY().toInt()
                        mScrollBottom = getY().toInt() + height
                    }

                    layout(mScrollLeft, mScrollTop, mScrollRight, mScrollBottom)
                    hasScroll = true
                    return true
                }
            }

            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                if (isScroll) {
                    isScroll = false
                    //重置点击状态
                    isPressed = false
                }
            }

            else -> {
            }
        }
        return super.onTouchEvent(ev)
    }
}